home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / libtiff / tif_next.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  4KB  |  150 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_next.c,v 1.14 92/02/10 19:06:45 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * NeXT 2-bit Grey Scale Compression Algorithm Support
  33.  */
  34. #include "tiffioP.h"
  35.  
  36. #if USE_PROTOTYPES
  37. static    int NeXTDecode(TIFF *, u_char *, int, u_int);
  38. #else
  39. static    int NeXTDecode();
  40. #endif
  41.  
  42. TIFFInitNeXT(tif)
  43.     TIFF *tif;
  44. {
  45.     tif->tif_decoderow = NeXTDecode;
  46.     tif->tif_decodestrip = NeXTDecode;
  47.     tif->tif_decodetile = NeXTDecode;
  48.     return (1);
  49. }
  50.  
  51. #define SETPIXEL(op, v) {            \
  52.     switch (npixels++ & 3) {        \
  53.     case 0:    op[0]  = (v) << 6; break;    \
  54.     case 1:    op[0] |= (v) << 4; break;    \
  55.     case 2:    op[0] |= (v) << 2; break;    \
  56.     case 3:    *op++ |= (v);       break;    \
  57.     }                    \
  58. }
  59.  
  60. #define LITERALROW    0x00
  61. #define LITERALSPAN    0x40
  62. #define WHITE       ((1<<2)-1)
  63.  
  64. static int
  65. NeXTDecode(tif, buf, occ, s)
  66.     TIFF *tif;
  67.     u_char *buf;
  68.     int occ;
  69.     u_int s;
  70. {
  71.     register u_char *bp, *op;
  72.     register int cc, n;
  73.     u_char *row;
  74.     int scanline;
  75.  
  76.     /*
  77.      * Each scanline is assumed to start off as all
  78.      * white (we assume a PhotometricInterpretation
  79.      * of ``min-is-black'').
  80.      */
  81.     for (op = buf, cc = occ; cc-- > 0;)
  82.         *op++ = 0xff;
  83.  
  84.     bp = (u_char *)tif->tif_rawcp;
  85.     cc = tif->tif_rawcc;
  86.     scanline = tif->tif_scanlinesize;
  87.     for (row = buf; occ > 0; occ -= scanline, row += scanline) {
  88.         n = *bp++, cc--;
  89.         switch (n) {
  90.         case LITERALROW:
  91.             /*
  92.              * The entire scanline is given as literal values.
  93.              */
  94.             if (cc < scanline)
  95.                 goto bad;
  96.             bcopy(bp, row, scanline);
  97.             bp += scanline;
  98.             cc -= scanline;
  99.             break;
  100.         case LITERALSPAN: {
  101.             int off;
  102.             /*
  103.              * The scanline has a literal span
  104.              * that begins at some offset.
  105.              */
  106.             off = (bp[0] * 256) + bp[1];
  107.             n = (bp[2] * 256) + bp[3];
  108.             if (cc < 4+n)
  109.                 goto bad;
  110.             bcopy(bp+4, row+off, n);
  111.             bp += 4+n;
  112.             cc -= 4+n;
  113.             break;
  114.         }
  115.         default: {
  116.             register int npixels = 0, grey;
  117.             int imagewidth = tif->tif_dir.td_imagewidth;
  118.  
  119.             /*
  120.              * The scanline is composed of a sequence
  121.              * of constant color ``runs''.  We shift
  122.              * into ``run mode'' and interpret bytes
  123.              * as codes of the form <color><npixels>
  124.              * until we've filled the scanline.
  125.              */
  126.             op = row;
  127.             for (;;) {
  128.                 grey = (n>>6) & 0x3;
  129.                 n &= 0x3f;
  130.                 while (n-- > 0)
  131.                     SETPIXEL(op, grey);
  132.                 if (npixels >= imagewidth)
  133.                     break;
  134.                 if (cc == 0)
  135.                     goto bad;
  136.                 n = *bp++, cc--;
  137.             }
  138.             break;
  139.         }
  140.         }
  141.     }
  142.     tif->tif_rawcp = (char *)bp;
  143.     tif->tif_rawcc = cc;
  144.     return (1);
  145. bad:
  146.     TIFFError(tif->tif_name, "NeXTDecode: Not enough data for scanline %d",
  147.         tif->tif_row);
  148.     return (0);
  149. }
  150.